package com.allenkerr.other;

import com.alibaba.fastjson.JSON;
import org.junit.Before;
import org.junit.Test;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.DoubleSummaryStatistics;
import java.util.LinkedList;
import java.util.List;

/**
 * Java 8
 * Created by 小柯 on 17/7/14.
 */
public class OtherTest03 {

    private List<User> users = null;

    @Before
    public void setup() {
        users = new LinkedList<>();
        users.add(new User("张三", 23, true, 2333.99));
        users.add(new User("李四", 24, true, 5444.99));
        users.add(new User("翠花", 22, false, 3444.99));
        users.add(new User("赵六", 34, true, 6444.99));
        users.add(new User("酸菜", 21, false, 5411.99));
    }

    @Test
    public void testStream1() {
        users.stream()
                .filter(user -> user.getSex())
                .filter(user -> user.getAge() < 30)
                .forEach(System.out::println);

        System.out.println("---------------------------------");

        DoubleSummaryStatistics doubleSummaryStatistics = users.stream()
                .filter(user -> user.getSex())
                .filter(user -> user.getAge() < 30)
                .mapToDouble(User::getSalary)
                .summaryStatistics();
        System.out.println(doubleSummaryStatistics);
    }

    @Test
    public void testStream2() {
        users.stream()
                .filter(user -> user.getSex())
                .forEach(user -> {
                    if (user.getAge() > 30) {
                        System.out.println("老人：" + user);
                        return;
                    }
                    System.out.println(user);
                });
    }

    @Test
    public void testNewTimeApi() {
        //查看当前的时区
        ZoneId defaultZone = ZoneId.systemDefault();
        System.out.println(defaultZone); //Asia/Shanghai

        //获取当前的时间
        LocalTime nowTime = LocalTime.now();
        System.out.println("nowTime = " + nowTime);

        Instant now = Instant.now(Clock.system(defaultZone));
        Instant oneDayAgo = now.plusMillis(-24 * 3600 * 1000);
        System.out.println("now = " + now);
        System.out.println("oneDayAgo = " + oneDayAgo);
    }

    public static class User {
        private String name;
        private Integer age;
        private Boolean sex;//男true，女false
        private Double salary;

        public User() {
        }

        public User(String name, Integer age, Boolean sex, Double salary) {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.salary = salary;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex=" + sex +
                    ", salary=" + salary +
                    '}';
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Boolean getSex() {
            return sex;
        }

        public void setSex(Boolean sex) {
            this.sex = sex;
        }

        public Double getSalary() {
            return salary;
        }

        public void setSalary(Double salary) {
            this.salary = salary;
        }
    }

}
